<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class BrandService extends BaseService
{
    //品牌

    /**
     * 品牌分类
     * 1头部，2中间，3底部，4头部2
     */
    protected $blank_types = [
        1 => '头部', '中间', '底部', '头部2',
    ];

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbBrand)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('sort', 'asc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有品牌');
        }
        $types = $this->blank_types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '';
            //商品
            $gname = '';
            $gOne = (array) DB::table($this->dbGoods)->where('id', $v['gid'])->first();
            if (!empty($gOne)) {
                $gname = $gOne['title'];
            }
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'type_name' => $type_name.'-'.$v['type_sort'],
                'cover_type' => $v['cover_type'],
                'cover' => env('DOMAIN') . '/' . $v['cover'],
                'gname' => $gname,
                'is_show' => $v['is_show'],
                'addtime' => $v['addtime'],
//                'type_sort' => $v['type_sort'],
                'sort' => $v['sort'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function add($pas)
    {
        $gid = 0;
        if (isset($pas['gno']) && $pas['gno']) {
            $gOne = (array) DB::table($this->dbGoods)->where('gno', $pas['gno'])->first();
            $gid = empty($gOne) ? 0 : $gOne['id'];
        }
        $lastid = DB::table($this->dbBrand)->insertGetId([
            'type' => $pas['type'],
            'cover_type' => $pas['cover_type'],
            'cover' => $pas['cover'],
            'gid' => $gid,
            'vlink' => $pas['vlink'],
            'detail' => htmlspecialchars($pas['detail']),
            'addtime' => toDate(),
        ]);
        $this->dbConn($this->dbBrand)->where('id', $lastid)->update(['type_sort' => $lastid, 'sort' => $lastid]);
        if (!$lastid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 修改
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $bOne = (array) DB::table($this->dbBrand)->where('id', $id)->first();
        if (empty($bOne)) {
            return array('code' => -2, 'msg' => '品牌不存在');
        }
        $gid = 0;
        if (isset($pas['gno']) && $pas['gno']) {
            $gOne = (array) DB::table($this->dbGoods)->where('gno', $pas['gno'])->first();
            $gid = empty($gOne) ? 0 : $gOne['id'];
        }
        $up = DB::table($this->dbBrand)->where('id', $id)
            ->update([
                'type' => $pas['type'],
                'cover_type' => $pas['cover_type'],
                'cover' => $pas['cover'],
                'gid' => $gid,
                'vlink' => $pas['vlink'],
                'detail' => htmlspecialchars($pas['detail']),
                'uptime' => toDate(),
            ]);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 详情
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) DB::table($this->dbBrand)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '品牌不存在');
        }
        $imgs = getimagesize($res['cover']);
        $res['domain'] = env('DOMAIN') . '/';
        $res['iw'] = 0; //$imgs[0];
        $res['ih'] = 0; //$imgs[1];
        $res['detail'] = htmlspecialchars_decode($res['detail']);
        $gOne = (array) DB::table($this->dbGoods)->where('id', $res['gid'])->first();
        $res['gno'] = empty($gOne) ? '' : $gOne['gno'];
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 删除
     * @param int $id
     */
    public function setDel($id)
    {
        $res = (array) DB::table($this->dbBrand)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '品牌不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = DB::table($this->dbBrand)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 显示隐藏
     */
    public function kgModify($id, $kg)
    {
        $res = (array) $this->dbConn($this->dbBrand)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbBrand)->where('id', $id)->update(['is_show' => $kg]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 类型排序设置
     */
    public function typeSort($id, $oper)
    {
        $dq = (array) $this->dbConn($this->dbBrand)
            ->where('id', $id)
            ->first();
        if (empty($dq)) {
            return $this->toback(-2, '没有记录');
        }
        $dqOnes = $this->dbConn($this->dbBrand)
            ->where('type_sort', $dq['type_sort'])
            ->get();
        $dqids = [];
        foreach ($dqOnes as $v) {
            $dqids[] = $v->id;
        }
        $dqSort = $dq['type_sort'];
        if ($oper == 1) {
            $operOne = (array) $this->dbConn($this->dbBrand)
                ->where('del', 0)
                ->where('type_sort', '<', $dq['type_sort'])
                ->orderBy('type_sort', 'desc')
                ->first();
            if (empty($operOne)) {
                return $this->toback(-2, '已在最前面了');
            }
        } else {
            $operOne = (array) $this->dbConn($this->dbBrand)
                ->where('del', 0)
                ->where('type_sort', '>', $dq['type_sort'])
                ->orderBy('type_sort', 'asc')
                ->first();
            if (empty($operOne)) {
                return $this->toback(-2, '已在最后面了');
            }
        }
        $operOnes = $this->dbConn($this->dbBrand)
            ->where('del', 0)
            ->where('type_sort', $operOne['type_sort'])
            ->get();
        $operids = [];
        foreach ($operOnes as $v) {
            $operids[] = $v->id;
        }
        $operSort = $operOnes[0]->type_sort;
        $this->dbConn($this->dbBrand)->whereIn('id', $dqids)->update(['type_sort' => $operSort]);
        $this->dbConn($this->dbBrand)->whereIn('id', $operids)->update(['type_sort' => $dqSort]);
        return $this->toback();
    }

    /**
     * 排序设置
     */
    public function sort($id, $oper)
    {
        $dq = (array) $this->dbConn($this->dbBrand)
            ->where('id', $id)
            ->first();
        if (empty($dq)) {
            return $this->toback(-2, '没有记录');
        }
        $dqSort = $dq['sort'];
        if ($oper == 1) {
            $operOne = (array) $this->dbConn($this->dbBrand)
                ->where('del', 0)
                ->where('type', $dq['type'])
                ->where('sort', '<', $dq['sort'])
                ->orderBy('sort', 'desc')
                ->first();
            if (empty($operOne)) {
                return $this->toback(-2, '已在最前面了');
            }
        } else {
            $operOne = (array) $this->dbConn($this->dbBrand)
                ->where('del', 0)
                ->where('type', $dq['type'])
                ->where('sort', '>', $dq['sort'])
                ->orderBy('sort', 'asc')
                ->first();
            if (empty($operOne)) {
                return $this->toback(-2, '已在最后面了');
            }
        }
        $operSort = $operOne['sort'];
        $this->dbConn($this->dbBrand)->where('id', $id)->update(['sort' => $operSort]);
        $this->dbConn($this->dbBrand)->where('id', $operOne['id'])->update(['sort' => $dqSort]);
        return $this->toback();
    }

    /**
     * 排序设置
     */
    public function sortModify($id, $sort)
    {
        $dq = (array) $this->dbConn($this->dbBrand)
            ->where('id', $id)
            ->first();
        if (empty($dq)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbBrand)->where('id', $id)->update(['sort' => $sort]);
        return $this->toback();
    }
}